﻿function doPage(siteId, module, channel, pageIdx, pageSize, wapSize = 2) {
  $("#ajaxElement_loading_" + channel).show();
  $("#ajaxElement_success_" + channel).hide();
  $("#ajaxElement_failure_" + channel).hide();

  $("#ajaxElement_wap_loading_" + channel).show();
  $("#ajaxElement_wap_success_" + channel).hide();
  $("#ajaxElement_wap_failure_" + channel).hide();

  //console.log("doPage");

  var url = "", templatorId = "";
  if (module == "shop") {
    url = "/api/model/getLoanShop";
    templatorId = "shopList";
  } else if (module == "prod") {
    url = "/api/model/getLoanSchema";
    templatorId = "loanList";
  } else if (module == "emp") {
    url = "/api/model/getLoanEmp";
    templatorId = "empList";
  }

  var param = {
    "page": pageIdx, "pageSize": pageSize, "siteId": siteId, "chId": channel
  };


  $.post(url, param, function (resp) {
    var res = resp.data;
    //console.log(res);
    var html = template(templatorId, { result: res.resData.dataList });
    var wapHtml = html;
    $("#ajaxElement_success_" + channel).html(html);
    if (pageSize != wapSize) {
      var wapDataList = [];
      for (var i = 0; i < res.resData.dataList.length; i++) {
        if (i < wapSize) {
          wapDataList.push(res.resData.dataList[i]);
        }
      }
      wapHtml = template(templatorId, { result: wapDataList });
    }
    $("#ajaxElement_wap_success_" + channel).html(wapHtml);

    //分页
    var pg = handlePage(pageIdx, res.resData.totalRec, param.pageSize, module, channel);
    //console.log(pg);
    //显示翻页
    if (pg.pageCount > 1) {
      var html2 = template('pageTpl', { page: pg });
      $("#ajaxElement_success_" + channel).append(html2);
      //$("#ajaxElement_wap_success_" + channel).append(html2);
    }

    $("#ajaxElement_loading_" + channel).hide();
    $("#ajaxElement_success_" + channel).show();
    $("#ajaxElement_failure_" + channel).hide();

    $("#ajaxElement_wap_loading_" + channel).hide();
    $("#ajaxElement_wap_success_" + channel).show();
    $("#ajaxElement_wap_failure_" + channel).hide();
  }, "json");
}

function handlePage(cur, totalRec, pageSize, module, channel) {
  var pg = { "cur": cur, "pageCount": 1, "pre": cur - 1, "next": cur + 1, "module": module, "channel": channel, "pageSize": pageSize };
  if (totalRec > pageSize) {
    var tPage = parseInt(totalRec / pageSize) + 1; //总页数
    pg.pageCount = tPage;
    if (pg.pre < 1) {
      pg.pre = 1;
    }
    if (pg.next > tPage) {
      pg.next = tPage;
    }
  }
  return pg;
}